package cn.bingo.yunsj.quartz;

import java.util.*;

import cn.bingo.yunsj.quartz.SchedulerConfigParam.SchedulerConfig;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.CronScheduleBuilder.cronSchedule;

public class MyScheduler {

    public static int groupNum = 0;

    public static int jobNum = 0;

    public static int triggerNum = 0;

    public static void main(String arg[])throws SchedulerException{
        LinkedList<String> list = new LinkedList<>();
//        list.add(SchedulerConfig.NEW_EMPLOYEE_RULE);
//        list.add(SchedulerConfig.YEAR_EMPLOYEE_RULE);
        Iterator i = list.iterator();
        while (i.hasNext()){
            String rule = i.next().toString();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        MyScheduler.doMyTask(rule);
                        System.out.println(123);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }).start();
        }

    }

    public static void doMyTask(String rule) throws SchedulerException
    {
        String myGroupNum = "myJob"+groupNum++;
        //创建一个JobDetail实例，将该实例与MyJob Class绑定。
        JobDetail jobDetail=JobBuilder.newJob(MyJob.class).withIdentity("myJob"+jobNum++
                , myGroupNum).build();
        //创建一个Trigger实例，定义该Job立即执行，并且每隔两秒钟重复一次，
        Trigger trigger=TriggerBuilder
                .newTrigger()
                .withIdentity("trigger"+triggerNum++,myGroupNum)
                .startNow()
//                .withSchedule(SimpleScheduleBuilder
//                        .simpleSchedule()
//                        .withIntervalInSeconds(5)
//                        .repeatForever())
                .withSchedule(cronSchedule(rule))
                .build();
        SchedulerFactory sf=new StdSchedulerFactory();
        Scheduler scheduler=sf.getScheduler();
        scheduler.start();
//        Date date=new Date();
//        SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//规范格式
//        //System.out.println("当前时间："+sfd.format(date));
        scheduler.scheduleJob(jobDetail, trigger);
    }
}